package com.tech.koufu.ui.activity;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.model.EntrustStock;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.CookieStringRequest;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.tools.Statics;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
/** 
 * @ClassName: DetailTransStocksActivity
 * @Description: TODO(交易明细详情页)  
 * @author 123
 * @date 2015年4月5日 下午5:12:45
 */
public class DetailTransStocksActivity extends Activity implements OnClickListener{
	private static final String TAG = DetailTransStocksActivity.class.getName();
	private Context mContext;
	private TextView txt_stock_name,txt_stock_code,txt_stock_time,txt_input_buy_stock_price,txt_input_buy_stock_num,
	txt_entrust_dealprice,txt_poundage;
	private Button btnBuy,btnSell;
	private UserStocks mUserStocks;
//	private RequestQueue mQueue; 
	private MyApplication myApp;
	CookieStringRequest stringRequest; 
	private EntrustStock stockInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_trans);
		mContext = this;
		myApp = (MyApplication) getApplication();
//		mQueue = Volley.newRequestQueue(getApplicationContext());
		initView();
	}
	
	 @Override
	protected void onResume() { 
		super.onResume();
		MobclickAgent.onResume(mContext);
	}
	 
	 @Override
	protected void onPause() { 
		super.onPause();
		MobclickAgent.onPause(mContext);
	}
	 
	private void initView(){
		findViewById(R.id.img_callback).setOnClickListener(this);
		((TextView)findViewById(R.id.tv_title)).setText(R.string.btn_trans_details);
		btnBuy = (Button)findViewById(R.id.btn_buy);
		btnSell = (Button)findViewById(R.id.btn_sell);
		txt_stock_name = (TextView)findViewById(R.id.txt_stock_name);
		txt_stock_code = (TextView)findViewById(R.id.txt_stock_code);
		txt_stock_time = (TextView)findViewById(R.id.txt_stock_time);
		txt_input_buy_stock_price = (TextView)findViewById(R.id.txt_input_buy_stock_price);
		txt_input_buy_stock_num = (TextView)findViewById(R.id.txt_input_buy_stock_num);
		txt_entrust_dealprice = (TextView)findViewById(R.id.txt_entrust_dealprice);
		txt_poundage = (TextView)findViewById(R.id.txt_poundage);
		mUserStocks = (UserStocks)getIntent().getSerializableExtra("userStock");
		showData();
	}
	private void loadData(){
//		String url = Statics.URL_2+"Stocks.dll";
//		final String t = KouFuTools.readTimeInfo(new String());
//		stringRequest = new CookieStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
//
//			@Override
//			public void onResponse(String response) { 
//				
//			}
//		}, new Response.ErrorListener() {
//
//			@Override
//			public void onErrorResponse(VolleyError error) { 
//				
//			}
//		}){
//			@Override
//			protected Map<String, String> getParams() throws AuthFailureError {
//				Map<String, String> params = new HashMap<String, String>();
//				params.put("userid", myApp.getUserid()); 
//				params.put("time", t);
//				return super.getParams();
//			}
//		};
	}
	private void showData(){
		if(mUserStocks!=null){
			txt_stock_name.setText(mUserStocks.getStockName());
			txt_stock_code.setText(mUserStocks.getStockId());
			//买入价
			String c = mUserStocks.stockbj;
	        c = String.format(getResources().getString(R.string.txt_input_buy_stock_price), c);
	        SpannableString style=new SpannableString(c);          
	        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bgBlackColor_63)),0,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
	        txt_input_buy_stock_price.setText(style);  
	    
	        //成交价
	       String bj = mUserStocks.stockbj;
	        c = String.format(getResources().getString(R.string.txt_entrust_dealprice), bj);
	        style=new SpannableString(c);          
	        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bgBlackColor_63)),0,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
	        txt_entrust_dealprice.setText(style);   
	        
	       //买入数量
	        String hold = mUserStocks.stockHold;
	        c = String.format(getResources().getString(R.string.txt_entrust_num), hold);
	        style=new SpannableString(c);          
	        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bgBlackColor_63)),0,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
	        txt_input_buy_stock_num.setText(style);  
	        
	      //成交价格 *成交数量 *佣金比例
	        float sxf = 0f;
	        if(null!=bj && null !=hold){
	        	float fbj = Float.parseFloat(bj);
	        	int num = Integer.parseInt(hold);
	        	 sxf = fbj*num*0.001f;
	        }
	        c = String.format(getResources().getString(R.string.txt_poundage), KouFuTools.df3.format(sxf)); 
	        txt_poundage.setText(c);  
	    
		}
		
        
    
	}
	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.img_callback:
			this.finish();
			break;
		case R.id.btn_buy:
			break;
		case R.id.btn_sell:
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if(keyCode == KeyEvent.KEYCODE_BACK){
			 this.finish();
			 return true;
		 }
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onStop() { 
		super.onStop(); 
	}

}
